
const key = "hashName";
module.exports = {
    async init() {
        let res = await Redis.getData(key);
        let hashName = res.data
        return hashName;
    },
    async login(data, new_sid){
        let Redis = require("../tools/redis.ts");

        var token = data.roomId;
        let is_token = await Redis.isKey("tokenList", token)
        if (!is_token) {
            return {
                code: 400,
                data: {},
                msg: "token有误或不存在！"
            }
        }
        let old_sid = await Redis.hGetValue("tokenList", token)
        if (old_sid) {
            await Redis.hDel("sidList", old_sid)
            return {
                code: 300,
                data: {
                    old_sid
                },
                msg: "有新的用户登录,请刷新重试"
            }
        }
        await Redis.hSet("sidList", new_sid, token)
        await Redis.hSet("tokenList", token, new_sid)
        await Redis.hSet(token, "sid", new_sid)
    },
    async delLoginInfo(sid){
        let token = await Redis.hGetValue("sidList", sid)
        if (token) {
            await Redis.hDel("sidList", sid)
            // await Redis.hDel("tokenList", token)
            await Redis.hSet("tokenList", token, "")
            await Redis.Del(token)
        }
    }
}
